<?php
session_start();
require('conn.php');
require('functions.php');
$tjd=$_COOKIE['tjd'];
//短信验证码
// $_SESSION['yzm1']=rand(1111,9999);
$currentTime = time();
$changeTime = 5*60;
$rand = '';
    if(isset($_SESSION['time'])) {
       if(($currentTime - $_SESSION['time']) >= $changeTime) {
            $_SESSION['time'] = $currentTime;
              $rand = mt_rand(1111, 9999);
              $_SESSION['yzm1'] = $rand;
       }else{
               $rand = $_SESSION['yzm1'];
       }
    }else{
        $_SESSION['time'] = $currentTime;
        $rand = mt_rand(1111, 9999);
        $_SESSION['yzm1'] = $rand;
    }
// var_dump( $rand);
//全站访问
if($config['fangwen']==4){
  exit('网站正在更新');
} 
if($_SESSION['login']==1){
  _location("ucenter.php",301);
  exit;
}

$ip=GetIP();

//地区限制
if($config['area']!=='全国'){
  require('ipclass.php');
  $iplocation = new IpLocation();   
  $location = $iplocation->getlocation($ip);   
  $country=to_utf8($location['country']);
  $config_country=$config['area'].$config['city'];
  $country2=str_replace('地区','',$config_country);
  if($country!==$country2){
    exit('当前城市非注册地区，禁止注册');
  }
}


//注册推荐人
if(is_numeric($tjd)){
  $tj_id=$tjd;
}else{
  $tj_id=guolv(trim($_POST['tj_id']));
}

if(is_numeric($tj_id)){
  $row_tj=$mysql->query("select * from `userdata` where `id` in({$tj_id})");
  if(!$row_tj){
    $tj_id=0;
  }
}else{
  $tj_id=0;
}

?>
<!doctype html>
<html>
<meta charset="utf-8">
<head>
<meta name="viewport" content="width=device-width,initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0" />
<title>注册</title>
<meta name="keywords" content="<?php echo $config['sitename']?>,用户注册。" />
<meta name="description" content="<?php echo $config['sitename']?>用户注册。">
<script type="text/javascript" src="<?php echo $site?>/static/jquery.js"></script>
<link href="<?php echo $site?>/static/all.css" type="text/css" rel="stylesheet" media="all">
<style>
input::-webkit-input-placeholder, textarea::-webkit-input-placeholder { 
    color:    #6b6b6b;
}
input:-moz-placeholder, textarea:-moz-placeholder { 
    color:    #6b6b6b;
}
input::-moz-placeholder, textarea::-moz-placeholder { 
    color:    #6b6b6b;
}
input:-ms-input-placeholder, textarea:-ms-input-placeholder { 
    color:    #6b6b6b;
}
body{margin:0;}
*{box-sizing:border-box;}
.bg{background: url('static/images/bg.png') center center;-webkit-background-size: 100px 100px;background-size: 100px 100px;-moz-background-size: 100px 100px;}
input{font-size: 16px;line-height: 1.25em;outline: 0px none;text-decoration: none;margin:0;}
</style>
</head>
<script type="text/javascript">
$(document).ready(function(){
  var InterValObj; //timer变量，控制时间
  var count = 5; //间隔函数，1秒执行
  var curCount=60;//当前剩余秒数  
  
  $("#btnSendCode").click(function(){
  var phone=document.getElementById("username").value; 
  // var _code = document.getElementById('code').value;
  if (!phone.match(/^(((1[3|4|5|7|8][0-9]{1}))+\d{8})$/)) {
    alert("手机号不正确");
    document.getElementById('username').focus();
    return;
  }
  var password=document.getElementById("password").value; 
  if(password==''){
    alert("请输入密码");
    document.getElementById('password').focus();
    return;   
  }
  var ifrm = document.getElementsByName('iframe')[0];  
  ifrm = document.getElementById('iframe');  
  ifrm.src = 'reg_yzm.php?phone='+phone;
  $("#btnSendCode").attr("disabled", "true");
  $("#btnSendCode").val("(" + curCount + "秒)");
  InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次 
  });
 //timer处理函数
function SetRemainTime() {
      if (curCount == 0) {                
        window.clearInterval(InterValObj);//停止计时器
        $("#btnSendCode").removeAttr("disabled");//启用按钮
        $("#btnSendCode").val("重新发送验证码");
        curCount=60;
      }
      else {
        curCount--;
        $("#btnSendCode").val("(" + curCount + "秒)");
      }
    }
});   
</script> 
<body class="bg" style="    overflow: hidden;">
<iframe name="iframe" style="display: none" id="iframe"></iframe>    
<div class="common-wrapper" style="background:transparent;top:13%">
  <div class="main">
    <form action="reg.php" method="post">
    <input type="hidden" value="<?php echo $_GET['uid']?>" name="tj_id">
      <div class="item clearfix" >
        <input value="" style="width:100%;border-color:#6b6b6b;color:#6b6b6b;float:left;background: none;" class="txt-input txtpd" name="username" id="username" placeholder="请输入手机号" type="text" />
      </div>
      <div class="item clearfix" style="margin-bottom: 1em;">
        <input value="" style="width:100%;margin-top: 1em;border-color:#6b6b6b;background: none;color:#6b6b6b;"  id="password" name="password" class="txt-input txtpd" placeholder="请输入密码" type="password" />
      </div>
      <div class="item clearfix" style="margin-bottom: 0.9em;">
        <input name="yzm" style="border-color:#6b6b6b;width:50%;float:left;background: none;color:#6b6b6b"  class="txt-input txtpd" id="yzm" placeholder="请输入手机验证码" type="text" />
        <input id="btnSendCode"style="color:#6b6b6b;width: 50%;float:left;line-height: 1px;border-radius:4px 4px 4px 0px;border:1px solid #6b6b6b;background:none"  class="btn-login" name="btnSendCode" type="button" value="发送验证码"/>
      </div>
      <br>
      <div class="item item-btns" style="margin-bottom: 0.9em;"> 
      <input style="background: #9a0000;line-height: 25px;border-radius: 4px;   box-shadow: 1px 2px 2px rgba(0,0,0,0.25);-webkit-box-shadow: 1px 2px 2px rgba(0,0,0,0.25);" type="submit" id="reg-btn"  value="注册" class="btn-login">
      </div>          
    </form>
    <div class="item item-login-option" style="margin-bottom: 0.9em;">
    <div style="width:100%;text-align:center"><a href="login.php" style="font-size:1em;color:#6b6b6b">已有帐号，点我登录</a></div>
    </div>
  </div>
</div>
<?php
  if($_POST){
  $phone=guolv(trim($_POST['username']));
  $pass=guolv(trim($_POST['password']));
  $yzm=guolv(trim($_POST['yzm']));
  // $code=guolv(trim($_POST['code']));
  //$yzm1 = substr($phone,3,4);
  // var_dump($_SESSION['yzm1']);
  // var_dump($yzm);
  
  if($config['openreg']==0){
    echo "<script>alert('注册关闭，请联系我们');location.href='reg.php'</script>";
    exit;   
  }
  if($config['ipreg2']==1){
    $row_ip=$mysql->query("select * from `userdata` where `ip`='{$ip}' order by `id` limit 1");//获取相同ip
    if($row_ip){
      echo "<script>alert('为防止同一人注册多个号,禁止同一ip重复注册!');location.href='reg.php'</script>";
      exit;     
    } 
  } 
  if(is_phone($phone)==false){
    echo "<script>alert('请输入正确的手机号');</script>";
    exit;
  }

  if($pass==''){
    echo "<script>alert('请输入密码');</script>";
    exit;   
  }

    if($yzm!=$_SESSION['yzm1']){
    echo "<script>alert('验证码错误');</script>";
    exit;   
    }

  $row=$mysql->query("select * from `userdata` where `phone`='{$phone}' limit 1");
  if(!$row){
    $arr=array(
      //'id'=>null,
      'tj_id'=>$tj_id,
      'phone'=>$phone,
      'pass'=>md5($pass),
      'money'=>$song,
      'money_all'=>$song,
      'wx'=>'',
      'realname'=>'',
      'alipay'=>'',
      'wgateid'=>'',
      'ip'=>$ip,
      'kou'=>100,
      'day'=>date("Y-m-d",time()),
      'time'=>date("Y-m-d H:i",time()),
    );
    //用户信息存到session中
    $_SESSION['phone']=$phone;
    $_SESSION['login']=1;
    setcookie("login",'1', time()+3600*24*365);
    // setcookie("phone",$phone, time()+3600*24*365);
    $value=arr2s($arr);
    $mysql->query("insert into `userdata` {$value}");
    //$mysql->query("INSERT INTO `userdata` (`id`, `tj_id`, `phone`, `pass`, `money`, `wx`, `realname`, `alipay`, `wgateid`, `ip`, `kou`, `day`, `time`) VALUES (NULL, '{$tj_id}', '{$phone}', '{$pass}', '{$song}', '', '', '', '', '{$ip}', '100', '{$day}', '{$time}');"); 
    $id=mysql_insert_id();
    if($id!==0){
      //师傅获得多少奖励
      if($config['reg_yzr1']!==0 && $tj_id!==0){
        $mysql->execute("update `userdata` set `money`=`money`+'{$config['reg_yzr1']}',`money_all`=`money_all`+'{$config['reg_yzr1']}',`money_xia`=`money_xia`+'{$config['reg_yzr1']}'  where `id`='{$tj_id}'");
        //写入状态
          $arr_reg_yzr1=array(
            'uid'=>$tj_id,
            'aid'=>'',
            'title'=>"新收徒弟ID：{$id}发放奖励",
            'long'=>'#',
            'money'=>$config['reg_yzr1'],
            'ip'=>$ip,
            'day'=>$day,
            'time'=>date("Y-m-d H:i",time()),
          );
        $value_reg_yzr1=arr2s($arr_reg_yzr1);
        $mysql->query("insert into `refererdata` {$value_reg_yzr1}");
      }
      echo "<script>alert('注册成功!');location.href='ucenter.php?phone={$phone}'</script>";
      exit;
    }else{
      echo "<script>alert('注册失败');'</script>";
      exit;     
    }
  }else{
    echo "<script>alert('手机号已经被占用');</script>";
    exit;   
  }
}
$mysql->__destruct();
$mysql->close();
?>
</body>
</html>